<?php 
require_once 'database/Database.php';
require_once 'database/DatabaseQuery.php';
require_once 'database/DatabaseUpdate.php';
require_once 'GlobalSettings.php';
require_once 'resource/FileResource.php';
require_once 'utility/Utility.php';
require_once  'utility/KeywordSearch.php';


class FileResourceManager
{
   protected static $instance = NULL;
   protected $searchEngine = null;
   
   public static function instance()
   {
       if(NULL == self::$instance)
	   {
	         self::$instance = new self();
	   }
	   
	   return self::$instance;
   }
	
   	protected function __construct()
	{	
	   $this->searchEngine =  new KeywordSearchEngine(
	Database::DB_COURSEWARE . ".file_resource"
	,"KEYWORDS"
	,"FileResource");
	$this->searchEngine->addOrderBy("REF_COUNT",false);
	//$this->searchEngine->addOrderBy("ID",false);
	//$this->searchEngine->addWhere("ID=73 or ID=71");
	}
	
	public function createFileResource($user,$url,$fileResourceInfoArray)
	{		
	    if($user->ID == NULL || $url == NULL)
		{
		  return NULL;
		}
		
		$fr=new FileResource();
		Utility::assignClassProps($fr,$fileResourceInfoArray);
		$fr->UPLOADER_ID = $user->ID;
		$fr->URL = $url;
		$fr->REF_COUNT = 0;		
		return $fr;
	}
	
	public function insertFileResource($fileResourceInstance)
	{
	   DatabaseUpdate::insertTable($fileResourceInstance);
	}
	
	public function search($searchArray,$searchState,$pageSize = GlobalSettings::SEARCH_PAGE_SIZE)
	{		
		return $this->searchEngine->search($searchArray,$searchState,$pageSize);
	}
	
	function loadFileResource($dbTableIns)//$dbTableInsArr could be object or array
	{
		return DatabaseQuery::loadTable($dbTableIns);
	}
}
?>